package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class SoftLightAction extends AbstractAction
	{
		private var contrast:Number;
		private var blur:Number;
		public function setValue(contrast:Number,blur:Number):SoftLightAction
		{
			this.contrast = contrast;
			this.blur = blur;
			return this;
		}
		
		public override function getDescription():String
		{
			return "SoftLight (" + this.contrast.toFixed(0) + ", " + this.blur.toFixed(0)+ ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			
			var matrix:ColorMatrix = new ColorMatrix();
			matrix.adjustContrast(contrast);
			var colorMatrixFilter1:ColorMatrixFilter = new ColorMatrixFilter(matrix.matrix);
			
			var blurFilter:BlurFilter = new BlurFilter(blur, blur, 1);
			var m:Array = [1, 0, 0, 0, 20, 0, 1, 0, 0, 20, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1];
			var colorMatrixFilter2:ColorMatrixFilter = new ColorMatrixFilter(m);
					
			ret.applyFilter(ret, ret.rect, new Point(0, 0), colorMatrixFilter2);
			ret.applyFilter(ret, ret.rect, new Point(0, 0), colorMatrixFilter1);
			ret.applyFilter(ret, ret.rect, new Point(0, 0), blurFilter);
			ret.merge(old, ret.rect, new Point(0, 0), 128, 128, 128, 128);
			return ret;
		}	
	}
}